# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
  layout "layouts/main" 
  
  before_filter :check_authentication #unless ENV['RAILS_ENV'] == 'test'
  before_filter :refresh_sprint
  
  helper :sort
  helper :collection_table
  
  # Used as a before_filter to ensure that the 'current_user' session variable
  # contians a valid User (or descendent class) object. Otherwise the user is
  # redirected to the login page. If redirected, the 'return-to' session
  # variable will contain the path to the page the user was originally trying to
  # access.
  def check_authentication
    unless session[:current_user].kind_of?(User) or
      self.class == SessionController

      session[:return_to] = request.request_uri
      flash[:status] = "Please log in, and we'll send you right along."
      redirect_to :controller => 'session', :action => 'login'
      return false
    end
  end  
  
  # make sure we are constantly refreshing our sprint data.
  def refresh_sprint
    if session[:sprint]
      session[:sprint].reload
    end
  end  
  
   # if we have an sprint in the session then filter to it.
  def filter_by_sprint(array_to_filter)
    filtered_array = []
    if session[:sprint]
      array_to_filter.each do |object|
        if object.sprint == session[:sprint]
          filtered_array << object
        end
      end
    else
      filtered_array = array_to_filter
    end
    filtered_array
  end  
end

# These various structures shouldn't have to be in application.rb!
class MilestoneStruct < ActionWebService::Struct
  member :name,         :string
  
  # we do comparisons based on the name.
  def == (other)
    other.name == self.name
  end
end

class TicketStruct < ActionWebService::Struct
  member :name,         :string
  member :ticket_id,    :string
  member :owner,         :string
  member :link,         :string
  
  # we do comparisons based on the id.
  def == (other)
    other.ticket_id == self.ticket_id
  end  
end

class UserStruct < ActionWebService::Struct
  member :name,         :string
  
  def == (other)
    other.name == self.name
  end
end